package com.gym.core.common;

import java.util.HashSet;
import java.util.Set;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 * @createTime 2015-1-6 上午11:46:14
 */
public class PinyinUtil {
	private static final Logger logger = LoggerFactory.getLogger(PinyinUtil.class);

	public static String[] toChinese(char src) {
		String[] pinyinArray =PinyinHelper.toHanyuPinyinStringArray(src);
		return pinyinArray;
	}
	
	
	public static String[] toChineseJustLetter(char src) {
		HanyuPinyinOutputFormat format= new HanyuPinyinOutputFormat();
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);  
		String[] pinyinArray = null;
		try {
			pinyinArray = PinyinHelper.toHanyuPinyinStringArray(src,format);
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			logger.error("error : ",e);
		}
		if(pinyinArray == null || pinyinArray.length == 0){
			return new String[]{String.valueOf(src)};
		}else if(pinyinArray.length == 1){
			return pinyinArray;
		} else {
			Set<String> resultSet = new HashSet<String>();
			for(String item : pinyinArray){
				resultSet.add(item);
			}
			return resultSet.toArray(new String[resultSet.size()]);
		}
	}
	
}
